<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue.js + Supabase 登录注册系统</title>
    <script src="vue.global.js"></script>
    <script src="supabase.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            width: 100%;
            max-width: 450px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            overflow: hidden;
        }
        
        .header {
            background: #4f46e5;
            color: white;
            padding: 25px 20px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 5px;
        }
        
        .header p {
            opacity: 0.9;
            font-size: 14px;
        }
        
        .form-container {
            padding: 30px;
        }
        
        .tabs {
            display: flex;
            margin-bottom: 25px;
            border-bottom: 1px solid #e5e7eb;
        }
        
        .tab {
            flex: 1;
            text-align: center;
            padding: 12px;
            cursor: pointer;
            font-weight: 500;
            color: #6b7280;
            transition: all 0.3s;
        }
        
        .tab.active {
            color: #4f46e5;
            border-bottom: 2px solid #4f46e5;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #374151;
        }
        
        input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            font-size: 16px;
            transition: border 0.3s;
        }
        
        input:focus {
            outline: none;
            border-color: #4f46e5;
            box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
        }
        
        button {
            width: 100%;
            padding: 14px;
            background: #4f46e5;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: background 0.3s;
        }
        
        button:hover {
            background: #4338ca;
        }
        
        button:disabled {
            background: #9ca3af;
            cursor: not-allowed;
        }
        
        .forgot-password {
            text-align: center;
            margin-top: 15px;
        }
        
        .forgot-password a {
            color: #4f46e5;
            text-decoration: none;
            font-size: 14px;
        }
        
        .forgot-password a:hover {
            text-decoration: underline;
        }
        
        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
            font-size: 14px;
        }
        
        .success {
            background: #d1fae5;
            color: #065f46;
            border: 1px solid #a7f3d0;
        }
        
        .error {
            background: #fee2e2;
            color: #991b1b;
            border: 1px solid #fecaca;
        }
        
        .user-info {
            text-align: center;
            padding: 20px;
        }
        
        .user-info h3 {
            margin-bottom: 10px;
            color: #4f46e5;
        }
        
        .user-info p {
            margin-bottom: 15px;
            color: #6b7280;
        }
        
        .logout-btn {
            background: #ef4444;
            width: auto;
            padding: 10px 20px;
        }
        
        .logout-btn:hover {
            background: #dc2626;
        }
        
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
            margin-right: 10px;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="container">
            <div class="header">
                <h1>Vue.js + Supabase</h1>
                <p>安全登录与注册系统</p>
            </div>
            
            <div class="form-container" v-if="!user">
                <div class="tabs">
                    <div class="tab" :class="{active: activeTab === 'login'}" @click="activeTab = 'login'">登录</div>
                    <div class="tab" :class="{active: activeTab === 'register'}" @click="activeTab = 'register'">注册</div>
                </div>
                
                <div v-if="message" class="message" :class="messageType">{{ message }}</div>
                
                <form @submit.prevent="handleSubmit">
                    <div class="form-group" v-if="activeTab === 'register'">
                        <label for="name">姓名</label>
                        <input type="text" id="name" v-model="form.name" placeholder="请输入您的姓名">
                    </div>
                    
                    <div class="form-group">
                        <label for="email">邮箱</label>
                        <input type="email" id="email" v-model="form.email" placeholder="请输入您的邮箱">
                    </div>
                    
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" id="password" v-model="form.password" placeholder="请输入您的密码">
                    </div>
                    
                    <button type="submit" :disabled="loading">
                        <span v-if="loading" class="loading"></span>
                        {{ activeTab === 'login' ? '登录' : '注册' }}
                    </button>
                </form>
                
                <div class="forgot-password">
                    <a href="#" @click.prevent="showForgotPassword = true">忘记密码？</a>
                </div>
                
                <div v-if="showForgotPassword" class="forgot-password-form">
                    <div class="form-group">
                        <label for="reset-email">邮箱</label>
                        <input type="email" id="reset-email" v-model="resetEmail" placeholder="请输入您的邮箱">
                    </div>
                    <button @click="resetPassword" :disabled="loading">发送重置链接</button>
                    <div class="forgot-password">
                        <a href="#" @click.prevent="showForgotPassword = false">返回登录</a>
                    </div>
                </div>
            </div>
            
            <div class="user-info" v-else>
                <h3>欢迎回来！</h3>
                <p>您已成功登录系统</p>
                <p><strong>邮箱:</strong> {{ user.email }}</p>
                <button class="logout-btn" @click="signOut">退出登录</button>
            </div>
        </div>
    </div>

    <script>
        const { createApp, ref, onMounted } = Vue;
        
        // 请替换为您的 Supabase 项目 URL 和公钥
        const SUPABASE_URL = 'http://192.168.0.210:8000';
        const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlzcyI6InN1cGFiYXNlIiwiaWF0IjoxNzYxNjY3MjAwLCJleHAiOjI5MTk0MzM2MDB9.T0EGDRqjvHyMtvotUdnFTtzLSC_9VnJZNt3oZtKC6oY';

        // 初始化 Supabase 客户端
        const supabase = window.supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
        
        createApp({
            setup() {
                const activeTab = ref('login');
                const form = ref({
                    name: '',
                    email: '',
                    password: ''
                });
                const user = ref(null);
                const message = ref('');
                const messageType = ref('');
                const loading = ref(false);
                const showForgotPassword = ref(false);
                const resetEmail = ref('');
                
                // 检查用户是否已登录
                const checkUser = async () => {
                    const { data } = await supabase.auth.getUser();
                    user.value = data.user;
                };
                
                // 处理表单提交
                const handleSubmit = async () => {
                    loading.value = true;
                    message.value = '';
                    
                    try {
                        if (activeTab.value === 'login') {
                            await signIn();
                        } else {
                            await signUp();
                        }
                    } catch (error) {
                        showMessage(error.message, 'error');
                    } finally {
                        loading.value = false;
                    }
                };
                
                // 用户登录
                const signIn = async () => {
                    const { data, error } = await supabase.auth.signInWithPassword({
                        email: form.value.email,
                        password: form.value.password
                    });
                    
                    if (error) throw error;
                    
                    user.value = data.user;
                    showMessage('登录成功！', 'success');
                    form.value.email = '';
                    form.value.password = '';
                };
                
                // 用户注册
                const signUp = async () => {
                    const { data, error } = await supabase.auth.signUp({
                        email: form.value.email,
                        password: form.value.password,
                        options: {
                            data: {
                                name: form.value.name
                            }
                        }
                    });
                    
                    if (error) throw error;
                    
                    showMessage('注册成功！请检查您的邮箱以验证账户。', 'success');
                    form.value.name = '';
                    form.value.email = '';
                    form.value.password = '';
                    activeTab.value = 'login';
                };
                
                // 退出登录
                const signOut = async () => {
                    const { error } = await supabase.auth.signOut();
                    if (error) {
                        showMessage(error.message, 'error');
                    } else {
                        user.value = null;
                        showMessage('您已成功退出登录', 'success');
                    }
                };
                
                // 重置密码
                const resetPassword = async () => {
                    if (!resetEmail.value) {
                        showMessage('请输入邮箱地址', 'error');
                        return;
                    }
                    
                    loading.value = true;
                    
                    const { error } = await supabase.auth.resetPasswordForEmail(resetEmail.value, {
                        redirectTo: window.location.href,
                    });
                    
                    if (error) {
                        showMessage(error.message, 'error');
                    } else {
                        showMessage('密码重置链接已发送到您的邮箱', 'success');
                        resetEmail.value = '';
                        showForgotPassword.value = false;
                    }
                    
                    loading.value = false;
                };
                
                // 显示消息
                const showMessage = (text, type) => {
                    message.value = text;
                    messageType.value = type;
                    
                    // 3秒后自动清除消息
                    setTimeout(() => {
                        message.value = '';
                    }, 3000);
                };
                
                // 监听认证状态变化
                supabase.auth.onAuthStateChange((event, session) => {
                    user.value = session?.user ?? null;
                });
                
                onMounted(() => {
                    checkUser();
                });
                
                return {
                    activeTab,
                    form,
                    user,
                    message,
                    messageType,
                    loading,
                    showForgotPassword,
                    resetEmail,
                    handleSubmit,
                    signOut,
                    resetPassword
                };
            }
        }).mount('#app');
    </script>
</body>
</html>